# from selenium.webdriver.common.keys import Keys
# from selenium import webdriver
# from lxml import etree
# import pandas as pd
# import time
# import random
# from geetest import Crack
# from urllib import parse
# urls = []
# products = []
# goods = []
#
# from company_info_sql import check_is_exit,update_data_to_db,insert_data_to_db
#
# def open_browser():
#     options = webdriver.ChromeOptions()
#     # # options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})  # 不加载图片,加快访问速度
#     options.add_experimental_option('excludeSwitches', ['enable-automation'])  # 开发者模式
#     # # options.add_argument('--headless') #实现不弹窗
#     options.add_argument('--disable-gpu')
#     browser = webdriver.Chrome(options=options, executable_path="./driver/chromedriver.exe")
#     browser.implicitly_wait(2)
#     browser.get('https://m.qichacha.com/')
#
#     return browser
#
#
# def open_url(ID,browser):
#     id = parse.quote(ID)
#     browser.get('https://m.qichacha.com/search?key='+ id)
#     search_result = len(browser.find_elements_by_class_name('list-item'))
#     if search_result >= 1:
#         link = browser.find_element_by_class_name('list-item')
#         link.click()
#         time.sleep(random.uniform(0.1,1))
#         switch_tab(browser, 2)
#         #browser.close()
#         if search_result == 1:
#             remarks = " "
#         else:
#             remarks = "多个搜索结果"
#         try:
#             browser.find_element_by_xpath('/html/body/div/p[1]')
#             temp_list = []
#             remarks = "多个搜索结果,但不支持显示该类型公司"
#             temp_list.append(["", "", ID, "", "", "", "", "", "", "", "", "", "", remarks])
#             df = pd.DataFrame(temp_list,
#                               columns=['现用公司名称', '曾用名', '名单名', '法定代表人', '注册资本', '电话', '邮箱', '统一社会信用代码', '企业类型', '所属行业',
#                                        '经营范围', '企业地址', '成立日期', '备注'])
#             return df, browser
#         except:
#             pass
#         df = craw1(browser, remarks, ID)
#
#     else:
#         try:
#             browser.get('https://m.qichacha.com/')
#             new_company = browser.find_element_by_class_name('list-item-name')
#             browser.execute_script("arguments[0].click();", new_company)
#             browser.find_element_by_xpath('//*[@id="popup-captcha-mobile"]/div/div[3]/div[1]')
#             crack = Crack(browser)
#             crack.crack()
#             df,browser = open_url(ID,browser)
#
#         except:
#             remarks = "无搜索结果"
#             temp_list= []
#             temp_list.append(["", "",ID, "", "", "", "", "","", "", "", "", "", remarks])
#             df = pd.DataFrame(temp_list, columns=['现用公司名称','曾用名','名单名', '法定代表人', '注册资本', '电话', '邮箱', '统一社会信用代码', '企业类型','所属行业', '经营范围','企业地址','成立日期', '备注'])
#     return df, browser
#
# def craw1(browser, remarks,ID):
#     browser.execute_script("window.scrollTo(0,document.body.scrollHeight);")
#     try:
#         browser.find_element_by_class_name('company-name')
#     except:
#         df, browser = open_url(ID, browser)
#     source = browser.page_source
#     hs = etree.HTML(source)
#     try:
#         represent = hs.xpath('/html/body/div[1]/div[2]/div[1]/div[2]/a/text()')[0].strip()
#         #(represent)
#     except IndexError:
#         represent = ""
#     capital = hs.xpath('//*[@id="base"]/div[2]/table/tbody/tr[3]/td[1]/div[2]/text()')[0].strip()
#     try:
#         phone = hs.xpath('/html/body/div[1]/div[2]/div[1]/div[3]/a[1]/text()')[0].strip()
#     except:
#         phone = ""
#     try:
#         mailbox = hs.xpath('/html/body/div[1]/div[2]/div[1]/div[3]/a[2]/text()')[0].strip()
#     except:
#         mailbox = ""
#
#     code = hs.xpath('//*[@id="base"]/div[2]/table/tbody/tr[6]/td[1]/div[2]/text()')[0].strip()
#     industry_type = hs.xpath('//*[@id="base"]/div[2]/table/tbody/tr[4]/td[1]/div[2]/text()')[0].strip()
#     #print(industry)
#     try:
#         industry = hs.xpath('//*[@id="base"]/div[2]/table/tbody/tr[11]/td/div[2]/text()')[0].strip()
#     except:
#         industry = ""
#     industry_range = hs.xpath('//*[@id="base"]/div[2]/table/tbody/tr[12]/td/div[2]/text()')[0].strip()
#     company_adress = hs.xpath('//*[@id="base"]/div[2]/table/tbody/tr[14]/td/div[2]/text()')[0].strip()
#     day = hs.xpath('//*[@id="base"]/div[2]/table/tbody/tr[2]/td[1]/div[2]/text()')[0].strip()
#     name = hs.xpath('//html/body/div[1]/div[2]/div[1]/div[1]/text()')[0].strip()
#     last_name = hs.xpath('//*[@id="base"]/div[2]/table/tbody/tr[10]/td/div[2]/text()')[0].strip()
#     #print(represent,capital,mailbox,phone,code,industry,industry_type,industry_range,company_adress,day,name,last_name)
#     temp_list = []
#     temp_list.append([name,last_name,ID, represent, capital, phone,mailbox, code, industry_type,industry, industry_range,company_adress,day, remarks])
#     df = pd.DataFrame(temp_list, columns=['现用公司名称','曾用名','名单名', '法定代表人', '注册资本', '电话', '邮箱', '统一社会信用代码', '企业类型','所属行业', '经营范围','企业地址','成立日期', '备注'])
#
#     return df
#
#
# def open_file(file_name):
#     global out_name
#     out_name = file_name.replace(".xlsx", '').replace(".xls", '') + "_out.xlsx"
#     data1=pd.read_excel(file_name,sheet_name=0)['企业名称']
#     #print(data1)
#     data2 = data1.dropna()
#     long = len(data2)
#     id_list = []
#     #print(data2[671])
#     for i in range(long):
#         try:
#             id = data2[i].strip()
#
#         except:
#             #print(i)
#             continue
#         skip_list = ["待装修","在装修","无牌","未装修、无人","未装修","无法进入，电梯封死","w无","无" ,"无人", "空", "无企业入驻", "双边有密码锁，未知是哪家公司", "物业","无入住门店",  "餐厅","无法进入","无公司","无企业牌照", "无牌无亮灯", "有人办公无牌照", "亮灯无牌", "无商户入驻"]
#         if id not in skip_list:
#             id_list.append(id)
#     return id_list
#
#
# if __name__ == '__main__':
#     file_name = "遗漏.xlsx"#input("请输入公司列表的文件名(输完请按回车键):\n")
#     #file_name = "写字楼.xlsx"
#     list_name = open_file(file_name)
#     #print(list_name)
#     dfs = []
#     browser = open_browser()
#     i = 1
#     n = len(list_name)
#     print("总计%d条" % n)
#     print("在程序运行中，不要打开文件，以及文件夹内的图片")
#
#     yinsheng_dict = {}
#     for index, ID in enumerate(list_name):
#
#         df, browser = open_url(ID, browser)
#         df = df[['现用公司名称', '注册资本', '电话', '邮箱', '企业类型', '企业地址', '成立日期']]
#         info = df.values[0]
#         #print("%f处理完成" % ID)
#         names = ['现用公司名称', '注册资本', '电话', '邮箱', '企业类型', '企业地址', '成立日期']
#
#         db_names = ['enterprise_name','register_capital','tel','email','enterprise_type','address','establish_date']
#         info_dict = {key:value for key,value in zip(db_names,info)}
#         yinsheng_dict[id] = info_dict['enterprise_name']
#         info_dict['enterprise_name'] = ID
#         db_company_info = check_is_exit(info_dict['enterprise_name'])
#         if db_company_info:
#             update_data_to_db(db_company_info,info_dict)
#             print("存在，进行跟新")
#         else:
#             if info_dict['enterprise_name'] != "":
#                 for key in ['enterprise_name','province','city','district','address','website','tel','email','register_capital','establish_date','enterprise_type']:
#                     if key not in info_dict:
#                         info_dict[key] = ""
#                 insert_data_to_db(info_dict)
#                 print("不存在，插入")
#             else:
#                 print("找不到")
#
#         print("已完成%.2f%%" % (i/n*100))
#         print(info_dict)
#         print("index:" + str(100 +index))
#         i += 1
#         time.sleep(random.random())
#     browser.quit()
#     print(yinsheng_dict)
#
#     print("程序运行完毕可以打开文件了")
